<?php

namespace App\Http\Controllers\admin\applet;

use App\Http\Controllers\Controller;
use App\Models\admin\AdminPermissionModel;
use App\Models\admin\AdminUserModel;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Hash;
use lxs\api\ApiResponse;
use lxs\tools\AuthToken;
use Mews\Captcha\Facades\Captcha;

class LoginController extends Controller
{

    /**
     * 登录
     */
    public function signIn(): JsonResponse
    {
        $data = [
            'username' => request('username'),
            'password' => request('password'),
            'captcha'  => request('captcha'),
        ];

        $rules = ['captcha' => 'required|captcha_api:' . request('key')];
        $validator = validator()->make(request()->all(), $rules);
        if ($validator->fails()) {
            return ApiResponse::error('验证码错误');
        } else {

            $user = AdminUserModel::where('username', $data['username'])->first();
            if (!$user) {
                return ApiResponse::error('用户不存在');
            }

            if (!Hash::check($data['password'], $user->password)) {
                return ApiResponse::error('密码错误');
            }

            if ($user->status == 0) {
                return ApiResponse::error('用户被禁用');
            }

            $res = [
                'login_ip'  => request()->getClientIp(),
                'login_at'  => Carbon::now(),
                'login_num' => $user->login_num + 1,
            ];
            AdminUserModel::where('id', $user->id)->update($res);

            $auth = new AuthToken();
            $role = $user->role;
            $permissions = AdminPermissionModel::pluck('code', 'id')->toArray();
            $userPermission = [];
            if ($role->is_super == 1) {
//                $userPermission = array_column($permissions, 'code');
            } else {
                foreach ($role->permissions as $v) {
                    $userPermission[] = $permissions[$v];
                }
            }
            $token = $auth->createToken($user->id, 'admin');
            return ApiResponse::success('登录成功', [
                'token'    => $token,
                'userInfo' => [
                    'userName'    => $user->username,
                    'photo'       => $user->avatar,
                    'isSuper'     => $role->is_super,
                    'permissions' => $userPermission,
                    'time'        => time(),
                ]
            ]);
        }

    }


    /**
     * 退出登录
     */
    public function signOut(): JsonResponse
    {
        $auth = new AuthToken();

        $auth->destroyToken();

        return ApiResponse::success('退出登录成功');
    }


    /**
     * 获取登录验证码
     */
    public function getLoginCode(): JsonResponse
    {
        return ApiResponse::success(Captcha::create('default', 'api'));
    }


    /**
     * 管理员身份认证
     */
    public function systemLoginAuth(): JsonResponse
    {
        $data = request()->input();

        $user = AdminUserModel::where('username', $data['username'])->first();
        if (!$user) {
            return ApiResponse::error('用户不存在');
        }

        if (!Hash::check($data['password'], $user->password)) {
            return ApiResponse::error('密码错误');
        }

        if ($user->status == 0) {
            return ApiResponse::error('用户被禁用');
        }

        return ApiResponse::success('提交成功', [
            'userInfo'    => $user,
        ]);
    }


}
